<?php defined('BASEPATH') || exit('No direct script access allowed');

/**
 * 销售管理控制器
 *
 * @return void
 */


class Teach extends Auth_Controller
{

    private $_data;

    public function __construct()
    {
        parent::__construct();

        $this->load->language('teacher');
        $this->load->language('apply');

        $this->load->model('user_model', '', TRUE);
        $this->load->model('teach_model', '', TRUE);

        $this->_data['consultants'] = $this->user_model->get_all_users();

    }

    // 列表
    public function teachers()
    {
        $this->_data['page_title'] = '教师列表';
        //获取所有教师
        $this->_data['teachers'] = $this->teach_model->get_all_teachers();
        $this->load->view('teach/teacher_lists',$this->_data);
    }

    // 新增教师
    public function teacher_create()
    {
        $this->_data['page_title'] = '新增教师';

        if (isset($_POST['save']))
        {
            if ($id = $this->save_teacher())
            {
                // $user = $this->user_model->find($id);
                // $log_name = (isset($user->display_name) && !empty($user->display_name)) ? $user->display_name : ($this->settings_lib->item('auth.use_usernames') ? $user->username : $user->email);
                // log_activity($this->current_user->id, sprintf(lang('us_log_create') ,$user->role_name) . ': '.$log_name, 'users');

//                Template::set_message(lang('teacher_created_success'), 'success');
                redirect('/teach/teachers');
            }
        }

       $this->load->view('teach/teacher_form',$this->_data);

    }

    // 查看
    public function teacher_view()
    {
        //获取ID
        $id = (int)$this->uri->segment(3);

        //获取线索消息
        $teacher = $this->teach_model->get_teacher_by_id($id);

        $this->_data['teacher'] = $teacher;

        $this->_data['page_title'] = $teacher->teacher_name.'基本信息';

        $this->load->view('teach/teacher_view',  $this->_data);
    }


    //编辑
    public function teacher_edit()
    {
        $id = (int)$this->uri->segment(3);

        if (empty($id)) {
            redirect('/teach/teachers');
        }

        if (isset($_POST['save'])) {
            if ($this->_save_teacher('update', $id)) {
                redirect('/teach/teachers');
            } else {
            }
        } elseif (isset($_POST['delete'])) {
            if ($this->_save_teacher->delete($id)) {
                redirect('/teach/teachers');
            } else {
            }
        }

        $this->_data['page_title'] = '教师信息编辑';


        $teacher = $this->teach_model->get_teacher_by_id($id);

        $this->_data['teacher'] = $teacher;

        $this->load->view('teach/teacher_form',$this->_data);

    }

    // 删除教师
    public function teacher_delete()
    {
        $id = $this->input->post('id');

        if(is_numeric($id)) {
            if($this->teach_model->delete_teacher($id)) {
                show_response(0,'删除成功');
            } else {
                show_response(1,'删除失败');
            }
        } else {
            show_response(2,'参数非法');
        }
    }

    private function _save_teacher($type='insert', $id=0)
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('teacher_name', 'lang:teacher_nickname', 'required|trim|max_length[20]');
        $this->form_validation->set_rules('teacher_nickname', 'lang:teacher_nickname', 'trim|max_length[20]');
        $this->form_validation->set_rules('address', 'lang:teacher_address', 'trim|max_length[255]');
        $this->form_validation->set_rules('phone', 'lang:teacher_phone', 'required|trim|is_numeric|max_length[11]');
        $this->form_validation->set_rules('tel', 'lang:teacher_tel', 'trim|max_length[20]');
        $this->form_validation->set_rules('sex', 'lang:teacher_sex', 'trim|is_numeric|max_length[1]');
        $this->form_validation->set_rules('idcard', 'lang:teacher_idcard', 'trim|max_length[30]');
        $this->form_validation->set_rules('email', 'lang:bf_email', 'trim|max_length[20]');
        $this->form_validation->set_rules('birthday', 'lang:teacher_birthday', 'trim|max_length[20]');
        $this->form_validation->set_rules('entry_date', 'lang:teacher_entry_date', 'trim|max_length[20]');
        $this->form_validation->set_rules('type', 'lang:teacher_type', 'required|trim|is_numeric|max_length[2]');
        $this->form_validation->set_rules('level', 'lang:teacher_level', 'trim|is_numeric|max_length[2]');
        $this->form_validation->set_rules('consultant', 'lang:teacher_consultant', 'trim|is_numeric|max_length[2]');


        if ($this->form_validation->run() === FALSE) {
            return FALSE;
        }

        unset($_POST['save']);

        if (isset($_POST['birthday']) && empty($_POST['birthday'])) $_POST['birthday'] = NULL;
        if (isset($_POST['entry_date']) && empty($_POST['entry_date'])) $_POST['entry_date'] = NULL;

        if ($type == 'insert') {
            $id = $this->teach_model->insert_teacher($_POST);
            $return = is_numeric($id);
        } elseif ($type == 'update') {
            $return = $this->teach_model->update_teacher($id, $_POST);
        }

        //
        //Events::trigger('save_user', $this->input->post());

        return $return;
    }

    // 课表
    public function classes()
    {
        $this->_data['page_title'] = '课程管理';

        $teacher_id = intval($this->input->get('teacher'));
        $student_id = intval($this->input->get('student'));

        $classes = array();

        $teacher_classes = $this->teach_model->get_teacher_classes($teacher_id,$student_id);

        foreach($teacher_classes as $k=>$class) {
            $days = explode(',',$class->days);
            $arr = array();
            foreach($days as $day) {
                $student_classes = $this->teach_model->get_class_students($class->teacher_id,$class->date,$student_id);
                $arr['students'] = $student_classes;
                $arr['date'] = $day;
            }
            $class->dayss = (object)$arr;
            $classes[$k] = $class;
        }
        $this->_data['classes'] = (object)$classes;
//        var_dump( $this->_data['classes']);
        $this->load->view('teach/class_lists',$this->_data);
    }

    // 新建课程
    public function class_create()
    {
        $this->_data['page_title'] = '新建课程';
        $this->_data['teachers'] = $this->teach_model->get_all_teachers();

        if (isset($_POST['save'])) {
            if ($id = $this->_save_class()) {
                redirect('teach/classes');
            }
        }

        $this->load->view('teach/class_form',$this->_data);
    }

    // 保存课程
    private function _save_class($type='insert',$id=0)
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('student_id', 'lang:student_name', 'required|is_numeric|trim|max_length[11]');
        $this->form_validation->set_rules('date', 'lang:class_date', 'required|trim|date');
        $this->form_validation->set_rules('time', 'lang:class_time', 'required|trim|max_length[255]');
        $this->form_validation->set_rules('grade', 'lang:grade', 'trim|is_numeric');
        $this->form_validation->set_rules('subject', 'lang:subject', 'trim|is_numeric');
        $this->form_validation->set_rules('teacher_id', 'lang:class_info', 'required|is_numeric|trim|max_length[11]');
        $this->form_validation->set_rules('remark', 'lang:other_info', 'trim|max_length[255]');
        $this->form_validation->set_rules('consultant', 'lang:teacher_consultant', 'required|trim|is_numeric|max_length[11]');

//        if ($this->form_validation->run() === false) {
//            return FALSE;
//        }
        unset($_POST['save']);

        $data = $_POST;

        $data['update_time'] = time();
        $data['week'] = date('W',strtotime($_POST['date']));
        $data['year'] = date('m',strtotime($_POST['date']));
        $data['month'] = date('Y',strtotime($_POST['date']));

        if ($type == 'insert') {
            $data['create_time'] = time();
            $id = $this->teach_model->insert_class($_POST);
            $return = is_numeric($id);
        } elseif ($type == 'update') {
            $return = $this->teach_model->update_class($id, $_POST);
        }

        return $return;
    }

    // 申请管理
    public function applies()
    {
        $this->_data['page_title'] = '申请管理';
        $applies = $this->teach_model->get_all_applies();
        $this->_data['applies'] = $applies;
        $this->load->view('teach/apply_lists',$this->_data);
    }

    // 新建申请
    public function apply_create()
    {
        $this->_data['page_title'] = '新建申请';

        if (isset($_POST['save'])) {
            if ($id = $this->_save_apply()) {
                redirect('teach/applies');
            }
        }

        $this->load->view('teach/apply_form',$this->_data);
    }

    // 保存申请
    private function _save_apply($type='insert',$id=0)
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('student', 'lang:student_name', 'required|is_numeric|trim|max_length[11]');
        $this->form_validation->set_rules('teacher_info', 'lang:teacher_info', 'trim|max_length[255]');
        $this->form_validation->set_rules('student_info', 'lang:student_info', 'trim|max_length[255]');
        $this->form_validation->set_rules('advice_info', 'lang:advice_info', 'trim|max_length[255]');
        $this->form_validation->set_rules('stay_info', 'lang:stay_info', 'trim|max_length[255]');
        $this->form_validation->set_rules('class_info', 'lang:class_info', 'trim|max_length[255]');
        $this->form_validation->set_rules('other_info', 'lang:other_info', 'trim|max_length[255]');
        $this->form_validation->set_rules('consultant', 'lang:teacher_consultant', 'required|trim|is_numeric|max_length[11]');

        if ($this->form_validation->run() === false) {
            return FALSE;
        }

        unset($_POST['save']);

        if ($type == 'insert') {
            $id = $this->teach_model->insert_apply($_POST);
            $return = is_numeric($id);
        } elseif ($type == 'update') {
            $return = $this->teach_model->update_apply($id, $_POST);
        }

        return $return;
    }

    // 获取所有学生
    public function get_all_students()
    {
        $ret = array();
        $word = trim($_GET['q']);
        $callback = $_GET['callback'];
        $this->db->select('student_name,student_id');
        $this->db->like('student_name',$word);
        $query = $this->db->get('students');
        $users = $query->result_array();
        // echo $this->db->last_query();
        echo $callback."(".json_encode($users).")";
    }
}